package com.gem.service.impl;

import com.gem.domain.TrainingOrder;
import com.gem.mapper.TrainingOrderMapper;
import com.gem.service.TrainingOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.NoTransactionException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Bravea❤ ·勇
 * Description: 实训订单service
 * create
 * Modified By:
 */
@Service
@Transactional
@SuppressWarnings("all")
public class TrainingOrderServiceImpl implements TrainingOrderService {

    @Autowired
    private TrainingOrderMapper trainingOrderMapper;

    @Override
    public List<TrainingOrder> selectAllTrainingOrder() {
        return trainingOrderMapper.selectAllTrainingOrder();
    }

    @Override
    public boolean addTrainingOrder(TrainingOrder t) {
        try {
            if(t.getTrainingOrderAgencyFee() == null){
                t.setTrainingOrderAgencyFee(0.0);
            }
            if(t.getTrainingOrderLiveFee() == null){
                t.setTrainingOrderLiveFee(0.0);
            }
            if (t.getTrainingOrderLiveFeeCase() == null){
                t.setTrainingOrderLiveFeeCase(0.0);
            }
            if (t.getTrainingOrderSchoolSubsidy() == null){
                t.setTrainingOrderSchoolSubsidy(0.0);
            }
            return trainingOrderMapper.addTrainingOrder(t);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public TrainingOrder selectTrainingOrderById(String training_order_id) {
        try {
            return trainingOrderMapper.selectTrainingOrderById(training_order_id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean deleteTrainingOrderById(String[] training_order_id) {
        try {
            boolean isDeleteSuccess = true;
            for (String c: training_order_id) {
                if (c != null && !c.trim().equals("")){
                    Integer trainingOrderId = Integer.parseInt(c);
                    if(!trainingOrderMapper.deleteTrainingOrderById(trainingOrderId)){
                        isDeleteSuccess = false;
                        break;
                    }
                }
            }
            if(isDeleteSuccess){
                return true;
            }else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Override
    public boolean modifyTrainingOrderById(TrainingOrder t) {
        try {
            if(t.getTrainingOrderAgencyFee() == null){
                t.setTrainingOrderAgencyFee(0.0);
            }
            if(t.getTrainingOrderLiveFee() == null){
                t.setTrainingOrderLiveFee(0.0);
            }
            if (t.getTrainingOrderLiveFeeCase() == null){
                t.setTrainingOrderLiveFeeCase(0.0);
            }
            if (t.getTrainingOrderSchoolSubsidy() == null){
                t.setTrainingOrderSchoolSubsidy(0.0);
            }
            return trainingOrderMapper.modifyTrainingOrderById(t);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public List<TrainingOrder> searchTrainingOrder(String training_order_name, String training_order_IDNumber, Date training_order_payDate) {
        try {
            training_order_name = "%"+ training_order_name +"%";
            training_order_IDNumber = "%" + training_order_IDNumber + "%";
            String str_training_order_payDate = "";
            if (training_order_payDate==null){
                str_training_order_payDate="%%";
            }else {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                str_training_order_payDate = "%" + sdf.format(training_order_payDate) + "%";
            }
            return trainingOrderMapper.searchTrainingOrder(training_order_name, training_order_IDNumber, str_training_order_payDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public PageInfo<TrainingOrder> selectTrainingOrderByPage(Integer currentPage, Integer pageSize) {
        PageInfo<TrainingOrder> pageInfo = null;
        try {
            PageHelper.startPage(currentPage, pageSize);
//            List<TrainingOrder> orders = trainingOrderDao.selectAllTrainingOrder();
            pageInfo = new PageInfo<>(trainingOrderMapper.selectAllTrainingOrder());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public List<TrainingOrder> selectAllTrainingOrderNoOther() {
        try {
            return trainingOrderMapper.selectAllTrainingOrderNoOther();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public PageInfo<TrainingOrder> selectTrainingOrderByPageNoOther(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        PageInfo<TrainingOrder> pageInfo = null;
        try {
            pageInfo = new PageInfo<>(trainingOrderMapper.selectAllTrainingOrderNoOther());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public PageInfo<TrainingOrder> selectMyTrainingOrderByPage(Integer currentPage, Integer pageSize, String personname) {
        PageHelper.startPage(currentPage, pageSize);
        PageInfo<TrainingOrder> pageInfo = null;
        try {
            pageInfo = new PageInfo<>(trainingOrderMapper.selectMyTrainingOrder(personname));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public List<Integer> findMid() {
        return trainingOrderMapper.findMid();
    }

    @Override
    public Integer countStu(Integer id) {
        return trainingOrderMapper.countStu(id);
    }

    @Override
    public Integer countStuWeek(Integer id) {
        return trainingOrderMapper.countStuWeek(id);
    }

    @Override
    public PageInfo<TrainingOrder> searchTrainingOrderByPage(Integer currentPage, Integer pageSize, String trainingOrderName, String trainingOrderIDNumber, Date trainingOrderPayDate) {
        PageHelper.startPage(currentPage, pageSize);
        PageInfo<TrainingOrder> pageInfo = null;
        try {
            pageInfo = new PageInfo<>(searchTrainingOrder(trainingOrderName,trainingOrderIDNumber,trainingOrderPayDate));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public List<TrainingOrder> searchTrainingOrderNoOther(String training_order_name, String training_order_IDNumber, Date training_order_payDate) {
        try {
            training_order_name = "%"+ training_order_name +"%";
            training_order_IDNumber = "%" + training_order_IDNumber + "%";
            String str_training_order_payDate = "";
            if (training_order_payDate==null){
                str_training_order_payDate="%%";
            }else {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                str_training_order_payDate = "%" + sdf.format(training_order_payDate) + "%";
            }
            return trainingOrderMapper.searchTrainingOrderNoOther(training_order_name, training_order_IDNumber, str_training_order_payDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public PageInfo<TrainingOrder> searchTrainingOrderByPageNoOther(Integer currentPage, Integer pageSize, String trainingOrderName, String trainingOrderIDNumber, Date trainingOrderPayDate) {
        PageHelper.startPage(currentPage, pageSize);
        PageInfo<TrainingOrder> pageInfo = null;
        try {
            pageInfo = new PageInfo<>(searchTrainingOrderNoOther(trainingOrderName,trainingOrderIDNumber,trainingOrderPayDate));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public List<TrainingOrder> searchMyTrainingOrder(String training_order_name, String training_order_IDNumber, Date training_order_payDate, String personname) {
        try {
            training_order_name = "%"+ training_order_name +"%";
            training_order_IDNumber = "%" + training_order_IDNumber + "%";
            String str_training_order_payDate = "";
            if (training_order_payDate==null){
                str_training_order_payDate="%%";
            }else {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                str_training_order_payDate = "%" + sdf.format(training_order_payDate) + "%";
            }
            return trainingOrderMapper.searchMyTrainingOrder(training_order_name, training_order_IDNumber, str_training_order_payDate,personname);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public PageInfo<TrainingOrder> searchMyTrainingOrderByPage(Integer currentPage, Integer pageSize, String trainingOrderName, String trainingOrderIDNumber, Date trainingOrderPayDate, String personname) {
        PageHelper.startPage(currentPage, pageSize);
        PageInfo<TrainingOrder> pageInfo = null;
        try {
            pageInfo = new PageInfo<>(searchMyTrainingOrder(trainingOrderName,trainingOrderIDNumber,trainingOrderPayDate,personname));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public boolean addTrainingOrderList(List<TrainingOrder> list) {
        try {
            boolean is = true;
            for (TrainingOrder trainingOrder : list) {
                if(trainingOrder.getTrainingOrderAgencyFee() == null){
                    trainingOrder.setTrainingOrderAgencyFee(0.0);
                }
                if(trainingOrder.getTrainingOrderLiveFee() == null){
                    trainingOrder.setTrainingOrderLiveFee(0.0);
                }
                if (trainingOrder.getTrainingOrderLiveFeeCase() == null){
                    trainingOrder.setTrainingOrderLiveFeeCase(0.0);
                }
                if (trainingOrder.getTrainingOrderSchoolSubsidy() == null){
                    trainingOrder.setTrainingOrderSchoolSubsidy(0.0);
                }
                if(!trainingOrderMapper.addTrainingOrder(trainingOrder)){
                    is = false;
                }
            }
            if (is){
                return true;
            }else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        } catch (NoTransactionException e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return false;
    }
}
